#!/bin/bash

software_dir="AnyDoor"
venv_name="amdgpu"
RUN_SCRIPT="run_${software_dir}.sh"

[ ! -d "$software_dir" ] && git clone https://github.com/ali-vilab/AnyDoor.git $software_dir

cd $software_dir

[ ! -d "$venv_name" ] && python3 -m venv $venv_name

source $venv_name/bin/activate

pip3 install -r requirements.txt
pip3 uninstall torch torchvision torchaudio xformers
pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/rocm6.0
pip3 install opencv-python einops gradio albumentations omegaconf transformers open_clip_torch pytorch_lightning huggingface-hub

[ -d "path" ] || mkdir "path"
sudo chown -R $(whoami):$(whoami) "path"
cd path
curl -o epoch=1-step=8687.ckpt "https://modelscope.cn/api/v1/models/bdsqlsz/AnyDoor-Pruned/repo?Revision=master&FilePath=epoch=1-step=8687-pruned.ckpt"
curl -o dinov2_vitg14_pretrain.pth "https://modelscope.cn/api/v1/models/bdsqlsz/AnyDoor-Pruned/repo?Revision=master&FilePath=dinov2_vitg14_pretrain.pth"

cd ..

pip3 uninstall -y pytorch_lightning
pip3 uninstall -y gradio
pip3 install pytorch_lightning==1.6.5
pip3 install gradio==3.50.2

if [ ! -f "$RUN_SCRIPT" ]; then
    cat > $RUN_SCRIPT <<EOF
#!/bin/bash
source $venv_name/bin/activate
export HSA_OVERRIDE_GFX_VERSION=11.0.0
python run_gradio_demo.py
EOF
    chmod +x $RUN_SCRIPT
fi

echo "安装完成"
